package xiongwei.jiang.config;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import xiongwei.jiang.properties.EhcacheProperties;
import xiongwei.jiang.properties.WXWorkProperties;
import xiongwei.jiang.service.EhcacheService;
import xiongwei.jiang.service.WXWorkService;
import xiongwei.jiang.service.impl.EhcacheServiceImpl;
import xiongwei.jiang.service.impl.WXWorkServiceImpl;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;

/**
 * 服务自动加载配置类
 *
 * @author 天之蓝
 */
@Configuration
@EnableConfigurationProperties(value = {WXWorkProperties.class, EhcacheProperties.class})
public class WXWorkAutoConfiguration {
    @Resource
    private EhcacheProperties ehcacheProperties;

    @Resource
    private WXWorkProperties wxWorkProperties;

    @Bean
    public EhcacheService ehcacheService() {
        return new EhcacheServiceImpl(ehcacheProperties);
    }

    @Bean
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        return restTemplate;
    }

    @Bean
    public WXWorkService wxWorkService(EhcacheService ehcacheService, RestTemplate restTemplate) {
        return new WXWorkServiceImpl(ehcacheService, restTemplate, wxWorkProperties);
    }
}
